import { getCommunityList, getDetail, getFilterValue, getHouseList } from '@/apis/house'
const changeName = (list1, list2) => {
  for (const k in list2) {
    if (Object.prototype.toString.call(list2[k]) === '[object Array]') {
      list1[k] = []
      changeName(list1[k], list2[k])
    } else if (Object.prototype.toString.call(list2[k]) === '[object Object]') {
      list1[k] = {}
      changeName(list1[k], list2[k])
    } else {
      if (k === 'label') {
        list1.text = list2[k]
        continue
      }
      if (list2[k] === 'null') {
        list1.children = [{ text: '不限' }]
      }
      list1[k] = list2[k]
    }
  }
}
const state = {
  communityList: [],
  houseList: [],
  detail: {},
  filterValue: {}
}
const getters = {
}
const mutations = {
  updataList (state, payload) {
    const list = [payload.area, payload.subway]
    changeName(state.communityList, list)
  },
  getHouseList (state, payload) {
    state.houseList = payload
  },
  setDetail (state, payload) {
    state.detail = payload
  },
  setFilterValue (state, payload) {
    state.filterValue = payload
  }
}
const actions = {
  async getCommunity (ctx, payload) {
    const { data } = await getCommunityList(payload)
    ctx.commit('updataList', data.body)
  },
  async getHouse (ctx, payload) {
    const { data } = await getHouseList(payload)
    ctx.commit('getHouseList', data.body.list)
    return data
  },
  async getDetails (ctx, payload) {
    const { data } = await getDetail(payload)
    await ctx.commit('setDetail', {})
    ctx.commit('setDetail', data.body)
  },
  async getFilterValue (ctx) {
    const { data } = await getFilterValue()
    ctx.commit('setFilterValue', data.body)
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
